package com.wd.health.view.fragment;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.wd.health.R;
import com.wd.health.app.App;
import com.wd.health.model.adapter.ByqAdapter02;
import com.wd.health.model.adapter.CircleAdapter01;
import com.wd.health.model.bean.ByqBean;
import com.wd.health.model.bean.KslbBean;
import com.wd.health.utils.RetrofitManager;
import com.facebook.drawee.view.SimpleDraweeView;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.List;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

public class CircleFragment extends Fragment {


    private RecyclerView rv01;
    private int mid = 7;
    private XRecyclerView rv02;
    private SimpleDraweeView sdv_user;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_circle, container, false);
        initView(view);
        return view;
    }

    private void initView(View view) {
        rv01 = view.findViewById(R.id.rv_byq);
        rv02 = view.findViewById(R.id.rv_byq02);
        sdv_user = view.findViewById(R.id.sdv_user);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext(), RecyclerView.HORIZONTAL, false);
        rv01.setLayoutManager(linearLayoutManager);
        rv02.setLayoutManager(new LinearLayoutManager(getContext()));
        rv02.setPullRefreshEnabled(false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initRv01();
        initRv02();
        SharedPreferences user = App.getAppContext().getSharedPreferences("user", Context.MODE_PRIVATE);
        String headPic = user.getString("headPic", "");
        sdv_user.setImageURI(headPic);
    }

    private void initRv02() {
        RetrofitManager.getInstance().getByqData(mid, 1, 10, new Observer<ByqBean>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(ByqBean byqBean) {
                List<ByqBean.ResultBean> list = byqBean.getResult();
                ByqAdapter02 byqAdapter02 = new ByqAdapter02(list, getContext());
                rv02.setAdapter(byqAdapter02);
            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {

            }
        });
    }

    private void initRv01() {
        RetrofitManager.getInstance().getKslbData(new Observer<KslbBean>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(KslbBean kslbBean) {
                List<KslbBean.ResultBean> list = kslbBean.getResult();
                final CircleAdapter01 circleAdapter01 = new CircleAdapter01(list, getContext());
                rv01.setAdapter(circleAdapter01);
                circleAdapter01.setGetListener(new CircleAdapter01.GetListener() {
                    @Override
                    public void onClick(int position, int id) {
                        circleAdapter01.setmPosition(position);
                        mid = id;
                        initRv02();
                        circleAdapter01.notifyDataSetChanged();
                    }
                });
            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {

            }
        });
    }
}